package cn.lds.j375.chlm.controller;

import cn.lds.j375.chlm.entity.Order;
import cn.lds.j375.chlm.entity.OrderDetails;
import cn.lds.j375.chlm.entity.User;
import cn.lds.j375.chlm.serivce.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Stream;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    private Logger log = LoggerFactory.getLogger(OrderController.class);

    /**
     * 添加订单到购物车
     * @param mids
     * @param ns
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/do_add_order")
    public String doAddOrder(Integer[] mids, Integer[] ns, HttpSession session, Model model){

        List<OrderDetails> orderDetails = orderService.establistOrderDetails(mids, ns);

        orderDetails.forEach(od->{
            log.info("od:{}",od);
        });
        //将订单数据放入session
        session.setAttribute("ods",orderDetails);

        model.addAttribute("ods",orderDetails);

        return "order";
    }

    /**
     * 确认订单
     * @param session
     * @return
     * @throws Exception
     */
    @RequestMapping("/confirm_order")
    public String confirmOrder(HttpSession session) throws Exception {

        User loginUser = (User) session.getAttribute("loginUser");

        List<OrderDetails> orderDetails = (List<OrderDetails>) session.getAttribute("ods");

//        orderService.addOrder(loginUser.getId(),orderDetails);
        orderService.addOrder(loginUser.getId(),orderDetails);
        return "home";


    }

}
